//
//  FNNewsDetailFrame.m
//  FourNews
//
//  Created by xmg on 16/3/31.
//  Copyright © 2016年 天涯海北. All rights reserved.
//
#define FNNewsDetailTopBorder 15
#define FNNewsDetailLeftBorder 10
#define FNNewsDetailAltBorder 3

#import "FNNewsDetailContImgItem.h"
#import "FNNewsDetailFrame.h"

@implementation FNNewsDetailFrame

- (NSMutableArray *)pictureFs
{
    if (!_pictureFs){
        self.pictureFs = [[NSMutableArray alloc] init];
    }
    return _pictureFs;
}

- (NSMutableArray *)altFs
{
    if (!_altFs){
        self.altFs = [[NSMutableArray alloc] init];
    }
    return _altFs;
    
}

- (void)setDetailItem:(FNNewsDetailItem *)detailItem
{
    _detailItem = detailItem;
    
    CGFloat titleX = FNNewsDetailLeftBorder;
    CGFloat titleY = FNNewsDetailTopBorder;
    CGSize titleSize = [detailItem.title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
    _titleF = (CGRect){titleX,titleY,titleSize};
    
    CGFloat pTimeX = FNNewsDetailLeftBorder;
    CGFloat pTimeY = FNNewsDetailLeftBorder + FNNewsDetailTopBorder + titleSize.height;
    CGSize pTimeSize = [detailItem.ptime sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    _pTimeF = (CGRect){pTimeX,pTimeY,pTimeSize};
    
    CGFloat sourceX = 2*FNNewsDetailLeftBorder + pTimeSize.width;
    CGFloat sourceY = pTimeY;
    CGSize sourceSize = [detailItem.source sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    _sourceF = (CGRect){sourceX,sourceY,sourceSize};
    
//    _totalPicF = (CGRect){sourceX,sourceY,sourceSize};
    
    for (int i = 0; i<_detailItem.img.count; i++) {
        NSString *strSz = _detailItem.img[i][@"pixel"];
        NSInteger loc = [strSz rangeOfString:@"*"].location;
        NSString *strW = [strSz substringToIndex:loc];
        NSString *strH = [strSz substringFromIndex:loc+1];
        NSInteger imgH = [strH intValue] * ((FNScreenW-2*FNNewsDetailLeftBorder)/[strW intValue]);
        CGFloat topImgX = FNNewsDetailLeftBorder;
        CGFloat topImgY = pTimeY + pTimeSize.height + FNNewsDetailLeftBorder + _totalPicH;
        CGFloat topImgW = FNScreenW - 2*FNNewsDetailLeftBorder;
        CGFloat topImgH = imgH;
        CGRect imgF = CGRectMake(topImgX, topImgY, topImgW, topImgH);
        [self.pictureFs addObject:[NSValue valueWithCGRect:imgF]];
        
        NSString *altStr = _detailItem.img[i][@"alt"];
        CGSize altSize = [altStr boundingRectWithSize:CGSizeMake(FNScreenW - 2*FNNewsDetailLeftBorder, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
        CGRect altF = (CGRect){FNNewsDetailLeftBorder,topImgY + topImgH + FNNewsDetailAltBorder,altSize};
        [self.altFs addObject:[NSValue valueWithCGRect:altF]];
        
        _totalPicH += (topImgH + altF.size.height + 2 * FNNewsDetailLeftBorder);
    }
//    if (_detailItem.img.count != 0) {
//        NSString *strSz = _detailItem.img[0][@"pixel"];
//        NSInteger loc = [strSz rangeOfString:@"*"].location;
//        NSString *strW = [strSz substringToIndex:loc];
//        NSString *strH = [strSz substringFromIndex:loc+1];
//        NSInteger imgH = [strH intValue] * ((FNScreenW-2*FNNewsDetailLeftBorder)/[strW intValue]);
//        CGFloat topImgX = FNNewsDetailLeftBorder;
//        CGFloat topImgY = pTimeY + pTimeSize.height + FNNewsDetailLeftBorder;
//        CGFloat topImgW = FNScreenW - 2*FNNewsDetailLeftBorder;
//        CGFloat topImgH = imgH;
//        CGRect imgF = CGRectMake(topImgX, topImgY, topImgW, topImgH);
//    }
    
//    CGFloat contentX = FNNewsDetailLeftBorder;
    
    
    CGFloat contentY = sourceY + sourceSize.height + _totalPicH + 2*FNNewsDetailLeftBorder;
//    NSLog(@"%@",_detailItem.body);
    CGRect contentRect = [detailItem.body boundingRectWithSize:CGSizeMake(FNScreenW - 2*FNNewsDetailLeftBorder, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    _contentF = (CGRect){FNNewsDetailLeftBorder,contentY,contentRect.size};
    
}

@end
